#include "ConfigLogic.h"
#include "cJSON.h"
#include "stdio.h"
#include "BluetoothMsgParser.h"
#include "ExecutorTask.h"
#include "24cxx.h"
void setTHDataToEeprom(THData data);
void config_set_state(u8 state);
void config_set_temp(u8 temp);
void config_set_temp1(u8 temp);
void config_set_humi(u8 humi);
void config_set_humi1(u8 humi);
void config_set_sunshine(u8 light);
void config_set_smoke(u8 smoke);

u8 config_get_state(void);
u8 config_get_temp(void);
u8 config_get_humi(void);
u8 config_get_temp1(void);
u8 config_get_humi1(void);
u8 config_get_sunshine(void);
u8 config_get_smoke(void);
int configTh(char* msg){

	//解析数据
	THData data;
	int result = parseTHData(&data,msg);
	if(!result) return 0;
	
	//打印数据
	//printf("state=%d,temp=%d..........\r\n",data.state,data.temp);
	
	//把这些数据写到执行模块
	setTHData(data);
		
	//存到eeprom
	setTHDataToEeprom(data);
	return 1;

}

void setTHDataToEeprom(THData data){

	if(!config_eepprom_init()) return;
	
	 config_set_state(data.state);
	 config_set_temp(data.temp);
	 config_set_temp1(data.temp1);
	 config_set_humi(data.humi);
	 config_set_humi1(data.humi1);
	 config_set_sunshine(data.sunshine);
	 config_set_smoke(data.smoke);
	
}
int configNetwork(char* msg){

	return 1;
}


int config_eepprom_init(){
	AT24CXX_Init();

	int times;
	while (AT24CXX_Check())
	{
		times++;
		if (times==200)
		{
			printf("AT24CXX_Check fail!\r\n");
			return  0;
		}
		
	}
	
	return 1;

}
void config_set_state(u8 state){
	//写数据
	AT24CXX_WriteOneByte(0,state);
}
void config_set_temp(u8 temp){
	//写数据
	AT24CXX_WriteOneByte(1,temp);
}
void config_set_temp1(u8 temp){
	
	//写数据
	AT24CXX_WriteOneByte(2,temp);
}
void config_set_humi(u8 humi){
	//写数据
	AT24CXX_WriteOneByte(3,humi);
}
void config_set_humi1(u8 humi){
	//写数据
	AT24CXX_WriteOneByte(4,humi);
}
void config_set_sunshine(u8 light){
	//写数据
	AT24CXX_WriteOneByte(5,light);
}
void config_set_smoke(u8 smoke){
	//写数据
	AT24CXX_WriteOneByte(6,smoke);
}

u8 config_get_state(void){
	return AT24CXX_ReadOneByte(0);
}
u8 config_get_temp(void){
	return AT24CXX_ReadOneByte(1);
}
u8 config_get_temp1(void){
	return AT24CXX_ReadOneByte(2);
}
u8 config_get_humi(void){
	return AT24CXX_ReadOneByte(3);
}

u8 config_get_humi1(void){
	return AT24CXX_ReadOneByte(4);
}
u8 config_get_sunshine(void){
	return AT24CXX_ReadOneByte(5);
}
u8 config_get_smoke(void){
	return AT24CXX_ReadOneByte(6);
}


void 	loadTHData(THData* data){
	data->state = config_get_state();
	data->temp = config_get_temp();
	data->temp1 = config_get_temp1();
	data->humi = config_get_humi();
	data->humi1 = config_get_humi1();
	data->sunshine = config_get_sunshine();
	data->smoke = config_get_smoke();
}